#ifndef _NETWORK_INFO_H_
#define _NETWORK_INFO_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <sys/select.h>
#include <fcntl.h>
#include <sys/file.h>
#include <sys/stat.h>
#include "../network_protocol/include/network_common.h"
#include "serial_uart.h"
#include "dictionary.h"
#include "iniparser.h"

#ifdef __cplusplus
extern "C" {
#endif
#define STR_MAX_SZIE (128)
#define IP_ADDR_MAX_LENGTH (16)
#define MAX_LINE  (256)
#define MAC_LENGTH (18)
#define CONFIG_DIR_NAME "/etc/conf/"
#define CONFIG_INI_NAME "/etc/conf/config.ini"
#define CONFIG_MEDIAMTX_YML "/mediamtx/mediamtx.yml"
typedef enum FILE_TYPE_E
{
    TYPE_DIR,
    TYPE_FILE
}FILE_TYPE_E;

typedef struct Str_S{
    char str[STR_MAX_SZIE];
} Str_S;

typedef struct UserConfig_S{
    char user_data[8];
    int size;
}UserConfig_S;

typedef struct UpgradeChip_S{
    char filename[128];
}UpgradeChip_S;
//     NetworkConfigCameraIrImageInfo pseudo_color;
//     NetworkConfigCameraIrImageZoom electronic_zoom;
//     NetworkConfigCameraIrCalibration shutter_calibration;
//     NetworkConfigAlgorithmImage outdoor_mode;
typedef struct NetworkCameraChipInfo{
    int pseudo_color;
    float electronic_zoom;
    int shutter_calibration;
    int outdoor_mode;
    int horizontal_image_flip;
    int vertical_image_flip;
    int analog_video_switch;
    int output_video_format;
    UserConfig_S get_user_config;
    UpgradeChip_S upgrade_chip_path;
}NetworkCameraChipInfo;

typedef enum {
    VALUE_TYPE_STRING = 0,
    VALUE_TYPE_INTEGER = 1,
    VALUE_TYPE_FLOAT = 2
} ValueType;

void *tcp_listener_thread(void *arg);
int create_empty_ini_file();
int write_netinfo_to_ini(void *st);
int write_chipctrl_to_ini(void *st);
int write_rtspurl_to_ini(void *ste);

int get_netinfo_from_ini(NetworkConfigNetworkTcpIp *net_info);
int get_chipctrl_from_ini(NetworkCameraChipInfo *chip_ctrl);
int get_rtspurl_from_ini(void *url);
int modify_yml(char *dstip);
double  get_memory_usage() ;
double get_cpu_usage();
int get_physical_addr(const char *interface, char *mac);
int set_network_config(const char *interface, const char *ip, const char *netmask, const char *gateway);

/**
 * @brief 通用INI配置存储函数
 * 
 * @param section 配置节名称
 * @param keys 键名数组
 * @param values 值数组
 * @param types 值类型数组 (0:字符串, 1:整数, 2:浮点数)
 * @param count 键值对数量
 * @return int 成功返回0，失败返回负值
 */
int write_config_to_ini(const char *section, const char **keys, const void **values, const int *types, int count);

/**
 * @brief 通用INI配置读取函数
 * 
 * @param section 配置节名称
 * @param keys 键名数组
 * @param values 值存储数组
 * @param types 值类型数组 (0:字符串, 1:整数, 2:浮点数)
 * @param default_values 默认值数组(可为NULL)
 * @param count 键值对数量
 * @return int 成功返回0，失败返回负值
 */
int read_config_from_ini(const char *section, const char **keys, void **values, const int *types, const void **default_values, int count);

/**
 * @brief 保存OSD配置信息到INI文件
 * 
 * @param osd_info OSD配置信息结构体
 * @return int 成功返回0，失败返回负值
 */
int save_osd_info_to_ini(const NetworkConfigOsdInfo *osd_info);

/**
 * @brief 从INI文件读取OSD配置信息
 * 
 * @param osd_info OSD配置信息结构体
 * @return int 成功返回0，失败返回负值
 */
int load_osd_info_from_ini( NetworkConfigOsdInfo *osd_info);

#ifdef __cplusplus
}
#endif

#endif